﻿
@{
    ViewBag.Title = "CivilPriceView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>土建成本</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入安装项目名称" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增引入</button>
        </div>
    </div>
</form>
<div id="QuoteCivilFeeLayer" style="display:none;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input type="text" id="QuoteSearchText" name="Title1" required lay-verify="required" onkeypress="QuoteGetKey()" placeholder="输入核价单名称" class="layui-input" style="width:300px;float:left;">
                <input type="text" value="" style="display:none" />
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="QuoteSearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="QuoteBtnAdd"><i class="layui-icon">&#xe61f;</i>引入选中项</button>
            </div>
        </div>
    </form>
    <table id="QuoteCivilFeeTable" lay-filter="QuoteCivilFeeTablefilter"></table>
</div>


<div class="layui-tab" lay-filter="tabFilter">
    <ul class="layui-tab-title">
        <li class="layui-this">外线部分</li>
        <li>红线内部分</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <table id="WX_CivilFeeTable" lay-filter="WX_CivilFeeTablefilter"></table>
        </div>
        <div class="layui-tab-item">
            <table id="HXN_CivilFeeTable" lay-filter="HXN_CivilFeeTablefilter"></table>
        </div>
    </div>
</div>


<script type="text/javascript">
    var table;
    var TabIndex = 0;
    var PreEstId = parent.$("#PreEstId").val();
    var PreEstType = parent.$("#ProjectType").val();
    layui.use(['form', 'table', 'upload', 'layer',  'element'], function () {
        var upload = layui.upload;
        var element = layui.element;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;
        var layerCopyIndex;
        var CivilPriceId;

        //监听选项卡切换
        element.on('tab(tabFilter)', function (data) {
            //index从0开始
            TabIndex = data.index;
        });


        $("#BtnAdd").on("click", function () {
            layerindex = layer.open({
                type: 1,
                title: ['设备成本基础数据', 'font-size:18px;'],
                area: '900px',
                offset: 'rt',
                content: $("#QuoteCivilFeeLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });
        });

        table.render({
            elem: '#QuoteCivilFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/PreEstimate/CivilTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Name', title: '项目名称', width: 200 }
                , { field: 'TaxRate', title: '税率', width: 100 }
                , { field: 'Unit', title: '单位', width: 100 }
                , { field: 'UnitPrice', title: '单价', width: 150 }
                , { field: 'Memo', title: '备注', width: 150 }

            ]]
        });


        //鼠标双击事件回调
        table.on('rowDouble(QuoteCivilFeeTablefilter)', function (obj) {
            var data = obj.data;
            index = layer.open({
                type: 2,
                area: ['1200px', '500px'],
                content: '/PreEstimate/CivilBriefView?CivilPriceId=' + data.CivilPriceId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
            //alert(data);
        });


        $("#QuoteBtnAdd").on("click", function () {
            var checkStatus = table.checkStatus('QuoteCivilFeeTable');
            var data = checkStatus.data;    //这里得到的data是array类型
            if (checkStatus.data.length == 0) { return; }
            //alert(JSON.stringify(data));
            //先检查内容
            data.forEach(function (item, index) {
                item["PreEstId"] = PreEstId;
                item["PreEstType"] = PreEstType;
                if (TabIndex == 0) {
                    item["TabType"] = "WX";
                }
                else if (TabIndex == 1) {
                    item["TabType"] = "HXN";
                }
            });

            $.ajax({
                type: "POST",
                url: "/Estimate/PECivilPriceAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('引入成功', { icon: 6, time: 1000 });
                        if (TabIndex == 0) {
                            table.reload('WX_CivilFeeTable');
                        }
                        else if (TabIndex == 1) {
                            table.reload('HXN_CivilFeeTable');
                        }

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });




        ///////////////////////////////////////////////////////////////////////
        //外线表格
        table.render({
            elem: '#WX_CivilFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PECivilTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"}
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Name', title: '项目名称', width: 200 }
                , { field: 'TaxRate', title: '税率', width: 60 }
                , { field: 'Unit', title: '单位', width: 60 }
                , { field: 'UnitPrice', title: '单价', width: 100 }                
                , { field: 'Quantity', title: '工程量', width: 80, edit: 'text'}
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.UnitPrice * d.Quantity).toFixed(2);
                    }
                }
                , { field: 'Memo', title: '备注', width: 150, edit: 'text'}
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });



        table.on("tool(WX_CivilFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                parent.layui.index.openTabsPage('/Estimate/CivilPriceSingleEdit?PECivilPriceId=' + data.PECivilPriceId, '项目信息编辑');

            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '”？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/CivilPriceDel",
                            async: false,
                            data: { "PECivilPriceId": data.PECivilPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("WX_CivilFeeTable", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

        //鼠标双击事件回调
        table.on('rowDouble(WX_CivilFeeTablefilter)', function (obj) {
            var data = obj.data;
            index = layer.open({
                type: 2,
                area: ['1200px', '500px'],
                content: '/Estimate/CivilBriefView?PECivilPriceId=' + data.PECivilPriceId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
            //alert(data);
        });

        table.on('edit(WX_CivilFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            //layer.alert('之前数据：' + JSON.stringify(TempEditData) + '。当前改变的数据：' + obj.value, {
            //    title: '当前行数据：'
            //});
            var data = obj.data;
            var field = obj.field;

            var Quantity = data.Quantity;
            var Memo = data.Memo;
            var PECivilPriceId = data.PECivilPriceId;

            try {
                if (field == "Quantity") {
                    if (Quantity == "") {
                        Quantity = 0;
                    }
                    else {
                        Quantity = eval(Quantity);
                    }
                }
            }
            catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('WX_CivilFeeTable');
                return;
            }
            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/CivilPriceUpdate_2",
                async: false,
                data: { "PECivilPriceId": PECivilPriceId, "Quantity": Quantity, "Memo": Memo },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('WX_CivilFeeTable');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        ///////////////////////////////////////////////////////////////////////
        //红线内表格
        table.render({
            elem: '#HXN_CivilFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PECivilTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='HXN' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Name', title: '项目名称', width: 200 }
                , { field: 'TaxRate', title: '税率', width: 60 }
                , { field: 'Unit', title: '单位', width: 60 }
                , { field: 'UnitPrice', title: '单价', width: 100 }
                , { field: 'Quantity', title: '工程量', width: 80, edit: 'text' }
                , {
                    title: '合价', width: 120,
                    templet: function (d) {
                        return (d.UnitPrice * d.Quantity).toFixed(2);
                    }
                }
                , { field: 'Memo', title: '备注', width: 150, edit: 'text' }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on("tool(HXN_CivilFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                parent.layui.index.openTabsPage('/Estimate/CivilPriceSingleEdit?PECivilPriceId=' + data.PECivilPriceId, '项目信息编辑');

            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '”？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/CivilPriceDel",
                            async: false,
                            data: { "PECivilPriceId": data.PECivilPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("HXN_CivilFeeTable", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

        //鼠标双击事件回调
        table.on('rowDouble(HXN_CivilFeeTablefilter)', function (obj) {
            var data = obj.data;
            index = layer.open({
                type: 2,
                area: ['1200px', '500px'],
                content: '/Estimate/CivilBriefView?PECivilPriceId=' + data.PECivilPriceId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
            //alert(data);
        });


        table.on('edit(HXN_CivilFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            //layer.alert('之前数据：' + JSON.stringify(TempEditData) + '。当前改变的数据：' + obj.value, {
            //    title: '当前行数据：'
            //});
            var data = obj.data;
            var field = obj.field;

            var Quantity = data.Quantity;
            var Memo = data.Memo;
            var PECivilPriceId = data.PECivilPriceId;

            try {
                if (field == "Quantity") {
                    if (Quantity == "") {
                        Quantity = 0;
                    }
                    else {
                        Quantity = eval(Quantity);
                    }
                }
            }
            catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('HXN_CivilFeeTable');
                return;
            }
            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/CivilPriceUpdate_2",
                async: false,
                data: { "PECivilPriceId": PECivilPriceId, "Quantity": Quantity, "Memo": Memo },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('HXN_CivilFeeTable');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });







    });


    //引用表搜索
    function QuoteGetKey() {
        if (event.keyCode == 13) {
            QuoteSearchFunc();
        }
    }
    function QuoteSearchFunc() {
        var text = $("#QuoteSearchText").val();
        if (text == "") {
            table.reload("QuoteCivilFeeTable", {
                where: {
                    SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1"
                }
            });
        }
        else {
            table.reload("QuoteCivilFeeTable", {
                where: {
                    SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1 and Name like '%" + text + "%'"
                }
            });
        }
    }



    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            if (TabIndex == 0) {
                table.reload("WX_CivilFeeTable", {
                    where: {
                        SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
            else if (TabIndex == 1) {
                table.reload("HXN_CivilFeeTable", {
                    where: {
                        SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='HXN' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }

        }
        else {
            if (TabIndex == 0) {
                table.reload("WX_CivilFeeTable", {
                    where: {
                        SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='WX' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "' and Name like '%" + text +"%'"
                    }
                });
            }
            else if (TabIndex == 1) {
                table.reload("HXN_CivilFeeTable", {
                    where: {
                        SortName: "PECivilPriceId", Order: "Desc", Condition: " and TabType='HXN' and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "' and Name like '%" + text + "%'"
                    }
                });
            }
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };




</script>
